<!--
  @Description:导航配置编辑
  @Author:zqy
  @Date:2022-12-23 16:20:35
  @Last Modified by:zqy
  @Last Modified time:2022-12-23 16:20:35
-->
<template>
  <el-dialog
    ref="refDlg"
    :title="`${!!(detail && detail.id) ? '编辑' : '新增'}导航基础信息配置`"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :hide-func-bar="true"
    :before-close="clearForm"
    width="500px"
    custom-class="custom-dlg"
    @close="closeDialog"
  >
    <div>
      <el-form ref="form" :model="formData" :rules="formRules" size="mini" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="导航名称" prop="name">
              <el-input v-model="formData.name" size="mini" placeholder="请输入导航名称" maxlength="15" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="类型"
              prop="type"
              :rules="[
                {required:true,message:'请选择',trigger:'change'}
              ]"
            >
              <el-select v-model="formData.type" style="width:100%" :disabled="!!(detail && detail.id)" @change="handleChangeType">
                <el-option value="1" label="导航" />
                <el-option value="2" label="分类" />
                <el-option value="3" label="目录" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="层级"
              prop="showLevel"
              :rules="[
                {required:true,message:'请选择',trigger:'change'}
              ]"
            >
              <el-select v-model="formData.showLevel" :disabled="!!(detail && detail.id)" style="width:100%">
                <el-option value="1" label="一层" />
                <el-option v-if="[1,'1',3,'3'].includes(formData.type)" value="2" label="二层" />
                <el-option v-if="[1,'1'].includes(formData.type)" value="3" label="三层" />
                <el-option v-if="[1,'1'].includes(formData.type)" value="4" label="四层" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="导航图标:" prop="picUrl">
              <div class="img-upload-container">
                <qup-img-uploader
                  :width="100"
                  :height="100"
                  mask
                  :image-url="iconImg.absoluteUrl"
                  :relative-url="iconImg.relativeUrl"
                  @upload-success="handleUploadIcon"
                  @remove="handleDelIcon"
                />
                <div class="icon-msg">建议尺寸100*100,不超过1M,支持png\jpg\jpeg</div>
              </div>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" type="primary" @click="submitForm">保存</el-button>
      <el-button size="mini" type="primary" @click="submitForm('config')">保存并去配置导航</el-button>
    </div>
  </el-dialog>
</template>
<script>
import QupImgUploader from '@/components/imgUploader'
import { saveTabDefine } from '@/api/tabDefine'
export default {
  name: 'TabConfigEditBasic',
  components: { QupImgUploader },
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detail: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      submiting: false,
      formData: {
        name: '',
        type: '', // 类型（首页导航：0，自定义导航：1，分类：2，目录：3）
        showLevel: '', // 层级类型 一级:1 二级:2 三级:3 四级：4
        picUrl: '',
        enableFlag: 1
      },
      formRules: {
        name: [
          { required: true, message: '请输入导航名称', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择类型', trigger: 'blur' }
        ],
        showLevel: [
          { required: true, message: '请选择层级', trigger: 'blur' }
        ]
      },
      iconImg: {
        absoluteUrl: '',
        relativeUrl: ''
      }
    }
  },
  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  watch: {

  },
  created() {
    if (this.detail && this.detail.id) {
      this.initFormData(this.detail)
    }
  },
  mounted() {

  },
  methods: {
    initFormData(data) {
      const enableFlag = [0, '0', 1, '1'].includes(data.enableFlag) ? data.enableFlag : 1
      this.formData = {
        name: data.name || '', // 导航名称
        type: `${data.type || ''}`,
        showLevel: `${data.showLevel}`,
        picUrl: data.picUrl,
        enableFlag
      }
      if (data.picUrl) {
        this.iconImg = {
          absoluteUrl: this.formatImageUrl(data.picUrl),
          relativeUrl: data.picUrl
        }
      }
    },
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        name: '',
        showLevel: '',
        type: '',
        picUrl: '',
        enableFlag: 1
      }
      this.iconImg = {
        absoluteUrl: '',
        relativeUrl: ''
      }
      this.$refs.form && this.$refs.form.clearValidate()
      done && done()
    },
    handleChangeType() {
      const { type, showLevel } = this.formData
      let isClear = false
      if ([2, '2'].includes(type)) {
        // isClear = [2, '2', 3, '3'].includes(showLevel)
        this.$set(this.formData, 'showLevel', '1')
      } else if ([3, '3'].includes(type)) {
        isClear = [3, '3'].includes(showLevel)
      }
      if (isClear) {
        this.$set(this.formData, 'showLevel', '')
      }
    },
    handleUploadIcon(res, file, fileList) {
      this.formData.picUrl = res.storeFileName
      this.iconImg = {
        absoluteUrl: res.url,
        relativeUrl: res.storeFileName
      }
    },
    handleDelIcon() {
      this.formData.picUrl = ''
      this.iconImg = {
        absoluteUrl: '',
        relativeUrl: ''
      }
    },
    // 提交表单处理
    submitForm(type) {
      if (this.submiting) return
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          const formData = Object.assign(this.formData)
          if (this.detail && this.detail.id) {
            formData.id = this.detail.id
          }

          this.submiting = true
          await saveTabDefine(formData).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message.success(`${formData.id ? '编辑' : '新增'}保存成功`)
              this.closeDialog()

              let data = {
                id: (this.detail && this.detail.id) || res.data,
                name: formData.name,
                type: formData.type,
                showLevel: formData.showLevel,
                picUrl: formData.picUrl
              }
              if (formData.id) {
                data = {
                  ...this.detail,
                  ...formData
                }
              }
              const result = {
                data,
                goConfig: type === 'config'
              }
              this.$emit('success', result)
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>
  .img-upload-container {
    .icon-msg{
      margin-top: 10px;
      color: #999;
    }
  }
</style>
